
import java.io.UnsupportedEncodingException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author TQNINH
 */
public class Ex_1 {
    
    
    public Ex_1() {
        
    }
    
    public static void main(String[] args) throws UnsupportedEncodingException, DataFormatException {
        // Encode a String into bytes
        String input = "The Deflater class compresses the data.";
        System.out.println("Test 1: " + input.length());
        byte[] in = input.getBytes("UTF-8");
        // Compress the bytes
        System.out.println("Test 2: " + in.length);
        byte[] output = new byte[100];
        Deflater deflater = new Deflater();
        deflater.setInput(in);
        deflater.finish();
        int compressDataLength = deflater.deflate(output);
        System.out.println("Test 3: " + compressDataLength);
        System.out.println("Test 4: " + output.toString());
        
        // Decompress the bytes
        //byte[] outputStr = new byte[1000];
        Inflater inflater = new Inflater();
        //inflater.setInput(outputStr, 0, outputStr.length);
        inflater.setInput(output, 0, output.length);
        byte[] result = new byte[100];
        int resultLength = inflater.inflate(result);
        inflater.end();
        // Decode the bytes into a String
        String outputSt = new String(result, 0, resultLength, "UTF-8");
        System.out.println("Test 5: " + outputSt);

    }
}
